<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/12 17:45
 * Blog：www.myblogs.xyz
 */

namespace app\api\controller;


use app\common\model\Member;
use app\common\model\Verify;
use app\common\service\SendService;

class Sms extends Common
{
    public function into()
    {
        $this->getInfo();
        if (empty($this->data['account'])){
            $this->jsonError('请填写手机号');
        }
        // 验证手机号是否注册
        if ($this->data['type'] == 2){
            $list_where = [];
            if (is_mobile($this->data['account'])){
                $list_where['mobile'] = $this->data['account'];
            }else if (is_email($this->data['account'])){
                $list_where['email'] = $this->data['account'];
            }else{
                $this->jsonError('发送失败');
            }
            $member_model = new Member();
            if (!$member_model->getExists($list_where)){
                $this->jsonError('该手机号尚未注册');
            }
        }
        if ($this->data['account'] == '18862928507' || $this->data['account'] == '16602856187'){
            $code = 123456;
        }else{
            $code = mt_rand(100000, 999999);
        }
        $day = date('Ymd');
        $verify_model = new Verify();
        $verify_where = ['account'=>$this->data['account'],'day'=>$day];
        $verify_data = $verify_model->getFind($verify_where);
        if (!empty($verify_data)){
            if (intval($verify_data['count']) > 5){
                $this->jsonError('您今天的验证吗获取次数过多');
            }
            $count = intval($verify_data['count']) + 1;
            $time = NEW_TIME + 360;
            $verify_model->where($verify_where)->update(['code'=>$code,'count'=>$count, 'time'=>$time]);
        }else{
            $verify_data = [
                'account'=>$this->data['account'],
                'day'=>$day,
                'type'=>$this->data['type'],
                'code'=>$code,
                'count'=>1,
                'time'=>NEW_TIME + 360
            ];
            $rows = $verify_model->insert($verify_data);
            if (empty($rows)){
                $this->jsonError('发送失败');
            }
        }
        $rows = SendService::verifyCode($this->data['account'], $code);
        if ($rows){
            $this->jsonSuccess('发送成功');
        }
        $this->jsonError('发送失败');
    }
}